home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
Z4SDBLD.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-09-05
|
7KB
|
249 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: z4sdbld.cpp
// Title: 9-Digit ZIP Code Directory -- on CD-ROM
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
// This module contains the program entry point for
//
// The code in this module may be written in C++ or C.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
//
//----------------------------------------------------------------------------
#include <z4.h>
//----------------------------------------------------------------------------
// Stack size
//----------------------------------------------------------------------------
#if COMPILER_BORLAND && (OS_DOS || OS_WINDOWS)
unsigned _stklen = 0x4000;
#endif
//----------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------
//
// A useless string which is simply embedded in the executable.
//
PSZ __pszCredits__ = "Written by John M Weeder, 1993";
//
// This should contain a program description which will be displayed as
// part of the program's help text.
//
static PCSZ pcszDescription =
"This program generates the state detail information for the CS file.";
//
// Program command line options
//
static DATACFG dcfg;
static CHAR szConfig[MAX_PATH] = "z4sd";
static BS_CMDOPT acmdopt[] =
{
{ "config", (PVOID)szConfig, CMDOPT_FILESPEC(80), "Configuration file name."},
BS_CMDOPT_NULL,
};
//
// Other globals
//
typedef int (*PFNQSORT)(const void *, const void *);
#define MAX_COUNTIES (300)
static CHAR aszCounty[MAX_COUNTIES][MAX_COUNTY+1];
static SIZET cCounties;
static SIZET cCities;
static LONG alZ5Count[Z4_ST_MAX];
//----------------------------------------------------------------------------
// Prototypes
//----------------------------------------------------------------------------
static BOOL FN_L StateDetail(Z4_STATE);
static BOOL FN_L StateWrite(Z4_STATE);
//----------------------------------------------------------------------------
// Description: Create the detail information for a single state.
// Detail information consists of the number of unique cities,
// ZIP codes and counties. A list of counties follows.
// Parameters: state State to find detail for.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
static BOOL FN_L StateDetail(Z4_STATE state)
{
Z4_CS_FILE cs_file;
Z4_CS cs;
if (!cs_file.Open()) // Open city/state file
return FALSE;
if (!Z4Z5CountRead(alZ5Count, sizeof(alZ5Count)))
return FALSE;
Output(" %s", Z4_ST_FILE::Abbreviation(state));
cCounties = 0;
cCities = 0;
if (cs_file.First(cs, state))
{
do
{
if (cs.state != state)
break;
Assert(cCities < 0xFFFF);
cCities++;
for (SIZET i = 0; i < cCounties; ++i)
if (strcmp(aszCounty[i], cs.szCounty) == 0)
break;
if (i >= cCounties && cs.szCounty[0])
{
Assert(cCounties < MAX_COUNTIES);
strcpy(aszCounty[cCounties], cs.szCounty);
cCounties++;
}
}
while (cs_file.Next(cs));
}
if (!cs_file.IsError() && cCities)
{
Output("\n");
return StateWrite(state); // Write information
}
else
Output("...not found");
Output("\n");
return !cs_file.IsError();
}
//----------------------------------------------------------------------------
// Description: Create the detail information for a single state.
// Detail information consists of the number of unique cities,
// ZIP codes and counties. A list of counties follows.
// Parameters: state State to find detail for.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
static BOOL FN_L StateWrite(Z4_STATE state)
{
static PCSZ pcszName = "DETAIL.$$$";
Z4_SD sd;
BYTE bVal = 0;
memset(&sd, 0, sizeof(sd));
qsort(aszCounty, cCounties, sizeof(aszCounty[0]), (PFNQSORT)strcmp);
sd.usCities = (USHORT)cCities;
sd.usZips = (USHORT)alZ5Count[state];
sd.usCounties = (USHORT)cCounties;
for (SIZET i = 0; i < cCounties; ++i)
sd.cbCounties += strlen(aszCounty[i]) + 1;
sd.cbCounties++; // Add for final null terminator
FLAG16 fs = FL_CREATE|FL_TRUNCATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
HF hf;
if (!FileOpen(&hf, pcszName, fs, NULL))
return FALSE;
if (!FileWrite(hf, &sd, sizeof(sd), 0))
{
FileClose(hf);
return FALSE;
}
for (i = 0; i < cCounties; ++i)
if (!FileWrite(hf, aszCounty[i], strlen(aszCounty[i]) + 1, -1))
{
FileClose(hf);
return FALSE;
}
if (!FileWrite(hf, &bVal, sizeof(bVal), 0))
{
FileClose(hf);
return FALSE;
}
FileClose(hf);
PCSZ pszSt = Z4_ST_FILE::Abbreviation(state);
BOOL fResult = DioAppendFile(NULL, pszSt, DFT_RANDOM, 0, pcszName);
FnameDelete(pcszName);
return fResult;
}
//----------------------------------------------------------------------------
// Description: main() - Program entry point
// Parameters: Standard C parameters
// Returns: DOS return code.
//----------------------------------------------------------------------------
int main(int argc, char **argv)
{
static BS_CFG cfg = CFG_DFT;
SHORT retval = 99;
//
// Initialize base library
//
BaseLibraryInitialize(argc, argv, &cfg);
BaseTitle("$Revision: 93.1 $", __DATE__, __TIME__, "ZIP+4 State Detail File Build");
if (!BaseTitleHelp(acmdopt, pcszDescription))
return 99;
DioSetDataPath(getenv("DATA"));
if (!DataConfigRead(szConfig, &dcfg))
{
Output("Failed reading configuration file.\n");
goto ERROR_EXIT;
} // Open for writing
if (!Z4_INQ::Start(Z4_INQ_CX|Z4_INQ_CS|Z4_INQ_ST|Z4_INQ_WRITE))
goto ERROR_EXIT;
Z4_STATE state;
for (state = Z4_ST_FIRST; state <= Z4_ST_LAST; ++(int)Z4_STATE state)
if (!StateDetail(state))
goto ERROR_EXIT;
retval = 0;
ERROR_EXIT:
Z4_INQ::Terminate();
DioCloseAll();
return retval;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------